package day3;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import day1.Student;

public class JDBCTest {

	public static void main(String[] args) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet  = null;
		try {
			//1、加载驱动Class.forName("");
			Class.forName("com.mysql.jdbc.Driver");
			//2、获得连接对象Connection
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java1912?useUnicode=true&characterEncoding=UTF-8", "root", "root");
			//3、写sql语句
			// String sql = "select id,name,age,gender from student where name like '张%' ";
			String sql = "select id,name,age,gender from student where name like ?";
			//4、创建Statement
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, "张%");
			//5、执行sql语句
			//     (1) 更新类（更改了表里面数据）：delete/update/insert     executeUpdate()
			//           返回值：int，表示你影响的行数
			//      (2)查询（没有改变表里面数据）:  select                  executeQuery()
			//          返回值：结果集ResultSet
			resultSet = preparedStatement.executeQuery();
			List<Student> list = new ArrayList<Student>();
			// 判断下一个有没有，如果有返回true，如果没有返回false
			while(resultSet.next()) {
				// 每遍历一次就可以封装一个Student
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
			
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			// 不管发没发生异常，finally都会执行，就可以把关闭数据库的操作放到这里
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			
		}
	}
	
	@Test
	public void test1() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select id,name,age,gender from student where name like ?";
			//String sql = "select id,name,age,gender from student where name=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, "张三%");
			System.out.println(preparedStatement);
			resultSet = preparedStatement.executeQuery();
			List<Student> list = new ArrayList<Student>();
			while(resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
			
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
	}
	
	
	@Test
	public void test2() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "insert into student(name,age,gender) values(?,?,?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, "张三123");
			preparedStatement.setInt(2, 23);
			preparedStatement.setString(3, "男");
			System.out.println(preparedStatement);
			int count = preparedStatement.executeUpdate();
			System.out.println("count: " + count);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
	}
	
	
	@Test
	public void test3() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "delete from student where id=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, 63);;
			System.out.println(preparedStatement);
			int count = preparedStatement.executeUpdate();
			System.out.println("count: " + count);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
	}
	
	
	
	@Test
	public void test4() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "update student set name=?,age=?,gender=? where id=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, "张三321");
			preparedStatement.setInt(2, 24);
			preparedStatement.setString(3, "女");
			preparedStatement.setInt(4, 62);
			System.out.println(preparedStatement);
			int count = preparedStatement.executeUpdate();
			System.out.println("count: " + count);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
	}
	
	
	
	
	
	
	
	
	
	
}
